package main

import "fmt"

// 本质是一个指针
type Animal interface {
	Sleep()
	GetColor() string
	GetType() string
}

type Cat struct {
	color   string
	typeStr string
}

// 定义具体类
func (c *Cat) Sleep() {
	fmt.Println("Cat is sleeping")
}

func (c *Cat) GetColor() string {
	return c.color
}

func (c *Cat) GetType() string {
	return c.typeStr
}

type Dog struct {
	color   string
	typeStr string
}

func (d *Dog) Sleep() {
	fmt.Println("Dog is sleeping")
}

func (d *Dog) GetColor() string {
	return d.color
}

func (d *Dog) GetType() string {
	return d.typeStr
}

func Sleep(anim Animal) {
	anim.Sleep()
}

// 万能类型
func myFunc(arg interface{}) {
	//类型断言
	value, ok := arg.(string)
	if !ok {
		fmt.Println("arg is not string")
	} else {
		fmt.Printf("arg is string, value is %s\n", value)
	}
}
func goInterface() {
	//多态
	var animal Animal
	animal = &Cat{"black", "cat"}
	Sleep(animal)

	animal = &Dog{"white", "dog"}
	Sleep(animal)

	myFunc("hello")
	myFunc(animal)
}
